package com.chilli.cosima.action.login;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.data.bean.TOperator;
import com.chilli.cosima.data.bean.TPower;
import com.chilli.cosima.service.LoginService;
import com.chilli.cosima.service.UserService;
import com.chilli.cosima.utils.MD5Encrypt;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction extends BaseSupport
{
	/**
	 * 注释内容
	 */
	private static final long serialVersionUID = 5210778318468553988L;

	private static Log log = LogFactory.getLog(LoginAction.class);

	private LoginService loginService;

	private UserService userService;

	private String username;

	private String password;

	private String randomstr;

	public String execute() throws Exception
	{
		if (isInvalid(getUsername()))
		{
			log.info("input username(" + getUsername() + ") is invalid.");
			return INPUT;
		}
		log.info("input username(" + getUsername() + ") is valid.");

		if (isInvalid(getPassword()))
		{
			log.info("input password is invalid.");
			return INPUT;
		}
		log.info("input password is valid.");
		if (isInvalid(getRandomstr()))
		{
			log.info("input verifyCode(" + getRandomstr() + ") is invalid.");
			return INPUT;
		}
		log.info("input verifyCode(" + getRandomstr() + ") is valid.");

		String randomStr = (String) (ActionContext.getContext().getSession()
				.get("random"));// 取得session保存中的字符串
		log.info("Correct verifyCode is :" + randomStr);
		// 下面就是将session中保存验证码字符串与客户输入的验证码字符串对比了
		if (isInvalid(randomStr) || !randomStr.equals(getRandomstr()))
		{
			log.info("The input verifyCode is not correct.");
			return INPUT;
		}
		log.info("The input verifyCode is correct.");

		TOperator operator = userService.findUserByName(getUsername());

		if (operator == null)
		{
			log.info("The operator which username is " + getUsername()
					+ " is not exist.");
			return INPUT;
		}
		log.info("The operator which username is " + getUsername()
				+ " is exist.");

		if (!(MD5Encrypt.encode(getPassword())).equals(operator.getLoginpwd()))
		{
			log.info("The password is not correct.");
			return INPUT;
		}
		log.info("The password is correct.");

		ActionContext.getContext().getSession().put("operator", operator);

		// 获取权限，生成权限树
		List<String> powerIdListWithoutButton = loginService
				.findPowerIdListWithoutButton(operator);

		List<String> powerUrlList = loginService
				.findPowerUrlListWithButton(operator);
		ActionContext.getContext().getSession().put("powerUrlList",
				powerUrlList);

		List<String> powerIdListWithButton = loginService
				.findPowerIdListWithButton(operator);
		ActionContext.getContext().getSession().put("powerIdListWithButton",
				powerIdListWithButton);

		TPower powerRoot = new TPower();
		powerRoot.setPowerid("0");
		Node node = new Node(powerRoot);
		node = loginService.buildTree(node, powerIdListWithoutButton);
		String menuTree = loginService.parseTree2Str(node);
		ActionContext.getContext().getSession().put("menuTree", menuTree);
		log.info("User login successfully.");
		return SUCCESS;

	}

	public String logout()
	{
		ActionContext.getContext().getSession().clear();
		HttpSession session = super.request.getSession();
		session.invalidate();
		session.setMaxInactiveInterval(1);
		log.info("User logout successfully.");
		return INPUT;
	}

	public String login()
	{
		log.info("Forward to the login page.");
		return INPUT;
	}

	private boolean isInvalid(String value)
	{
		return (value == null || value.length() == 0);
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getRandomstr()
	{
		return randomstr;
	}

	public void setRandomstr(String randomstr)
	{
		this.randomstr = randomstr;
	}

	public LoginService getLoginService()
	{
		return loginService;
	}

	public void setLoginService(LoginService loginService)
	{
		this.loginService = loginService;
	}

	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

}
